/**
 * Created by lcw-home on 2015/11/15.
 */
// prepare the form when the DOM is ready
var loading=$('#loading');
var ajaxOptions = {
    //beforeSubmit:validate,
    success:       formResponse,  // post-submit callback
    type:      'post',        // 'get' or 'post', override for form's 'method' attribute
    dataType:  'xml',        // 'xml', 'script', or 'json' (expected server response type)
    timeout:   10000
};
var validateOptions={
    submitHandler:function(form){
        form.ajaxSubmit(ajaxOptions);
    }
};

function validate(arr, form, options){

}
function formResponse(responseText, statusText, xhr, $form)  {
    if('success'==statusText){
        xmlResponse(responseText);
    }else{
        console.log(responseText);
    }
}

function xmlResponse(xmlStr){
    console.log(xmlStr);
    var xml=$(xmlStr).find("root");

    var result=xml.find("result");
    if(result && result.length>0){
        var type='info';
        switch (result.find("status").text()){
            case '200':
                type='success';
                break;
            case '400':
                type='error';
                break;
            case '500':
                type='warning';
                break
        }
        gritter_info(result.find('title').text(),result.find('content').text(),type);
    }

    var redirect=xml.find('redirect');
    if(redirect && redirect.length>0){
        var url=redirect.find("url").text();
        var time=redirect.find("time").text();
        var target=redirect.find("target").text();
        setTimeout(function(){
            window.location.href=url;
        },time*1000);
    }
}
function center(obj){
    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;
    var popupHeight = $(obj).height();
    var popupWidth = $(obj).width();
    $(obj).css({
        "position": "absolute",
        "top": (windowHeight-popupHeight)/2+$(document).scrollTop(),
        "left": (windowWidth-popupWidth)/2
    });
}
function gritter_info(title,content,type,image,time){
    if(!type||type=='')
        type='info';
    if(!image || image==''){
        image='Public/assets/mypic/gritter/'+type+'.png';
    }else if(image.indexOf('.')==-1){
        image='Public/assets/mypic/gritter/'+image+'.png';
    }
    if(!time)
        time=5;
    time=parseInt(time)*1000;
    var opt={
        title:title,
        text:content,
        image:image,
        sticky:false,
        time:3000,
        class_name:'gritter-'+type
    };
    $.gritter.add(opt);
}
function ajaxContent(url,data){
    if(!data)
    data='';
    $.post(url,data,xmlResponse,'xml');
}
$(function($) {
    //loading
    var delVal=50;
    function autoMove(){
        delVal++;
        if(delVal>400){
            delVal=50;
        }
        $(".mvBtn").css("left",delVal);
    }
    setInterval(autoMove,8);
    function autoTsq(){
        $(".mvSq").css("color","#F5FAFD");
        setTimeout(function(){$(".mvSq").eq(0).css("color","#29B6FF")},0);
        setTimeout(function(){$(".mvSq").eq(1).css("color","#29B6FF")},500);
        setTimeout(function(){$(".mvSq").eq(2).css("color","#29B6FF")},1000);
    }
    setInterval(autoTsq,1500);
    center(loading);
    $(window).resize(function(){
        center(loading);
    });
    //全局ajax动画
    $(document).ajaxStart(function(){
        loading.show();
    }).ajaxStop(function(){
        loading.hide();
    }).ajaxError(function(){
        gritter_info('提交失败','网络或系统问题，请重试。','error')
    });

    $('a.ajaxLink').on('click',function(){
        var url=$(this).attr('href');
        ajaxContent(url);
        return false;
    });
});